package com.fh.modules.dbms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.query.QueryWrapperUtil;
import com.fh.common.util.StringUtils;
import com.fh.modules.dbms.entity.DbmsDbsource;
import com.fh.modules.dbms.service.IDbmsDbsourceService;
import com.fh.modules.dbms.service.IDbmsDbtypeService;
import com.fh.modules.dbms.service.IDbmsViewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Title: Controller
 * @Description: 数据源管理
 * @author： code generator
 * @date： 2020-05-15
 * @version： V1.0
 */
@RestController
@RequestMapping("/dbms/dbmsDbsource")
@Slf4j
@Api("数据源管理")
public class DbmsDbsourceController {
    @Autowired
    private IDbmsDbsourceService dbmsDbsourceService;
    @Autowired
    private IDbmsDbtypeService dbmsDbtypeService;
    @Autowired
    private IDbmsViewService dbmsViewService;

    /**
     * 分页列表查询
     *
     * @param dbmsDbsource
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("分页列表查询")
    @AutoLog(value = "数据源管理分页查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryPageList(DbmsDbsource dbmsDbsource,
                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                HttpServletRequest req) {
        QueryWrapper<DbmsDbsource> queryWrapper = new QueryWrapper<DbmsDbsource>();
        dbmsDbsource.setDelFlag(CommonConstant.DEL_FLAG_0);
        QueryWrapperUtil.initQueryWrapper(queryWrapper, dbmsDbsource);
        Page<DbmsDbsource> page = new Page<DbmsDbsource>(pageNo, pageSize);
        IPage<DbmsDbsource> pageList = dbmsDbsourceService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    @GetMapping(value = "/listAll")
    @ApiOperation("分页列表查询")
    @AutoLog(value = "数据源管理分页查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryList(DbmsDbsource dbmsDbsource) {
        QueryWrapper<DbmsDbsource> queryWrapper = new QueryWrapper<DbmsDbsource>();
        dbmsDbsource.setDelFlag(CommonConstant.DEL_FLAG_0);
        QueryWrapperUtil.initQueryWrapper(queryWrapper, dbmsDbsource);
        List<DbmsDbsource> pageList = dbmsDbsourceService.list(queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param dbmsDbsource
     * @return
     */
    @PostMapping(value = "/add")
    @ApiOperation("添加")
    @AutoLog("添加数据源管理")
    public Result add(@RequestBody DbmsDbsource dbmsDbsource) {
        dbmsDbsourceService.save(dbmsDbsource);
        return Result.ok("添加成功");
    }

    /**
     * 编辑
     *
     * @param dbmsDbsource
     * @return
     */
    @PutMapping(value = "/edit")
    @ApiOperation("修改")
    @AutoLog("修改数据源管理")
    public Result edit(@RequestBody DbmsDbsource dbmsDbsource) {
        boolean ok = dbmsDbsourceService.updateById(dbmsDbsource);
        return ok ? Result.ok("修改成功") : Result.error("修改失败");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @ApiOperation("通过id删除")
    @AutoLog("删除数据源管理")
    public Result delete(@RequestParam(name = "id", required = true) String id) {
        dbmsDbsourceService.deleteEntityByDelFlag(id);
        return Result.ok("删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    @ApiOperation("批量删除")
    @AutoLog("批量删除数据源管理")
    public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        dbmsDbsourceService.deleteBatchEntityByDelFlag(ids);
        return Result.ok("批量删除成功");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    @ApiOperation("通过id查询")
    @AutoLog(value = "数据源管理通过id查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryById(@RequestParam(name = "id", required = true) String id) {
        DbmsDbsource dbmsDbsource = dbmsDbsourceService.getById(id);
        return Result.ok(dbmsDbsource);
    }


    @GetMapping(value = "/queryAllViewDiff")
    @ApiOperation("查询视图差异")
    @AutoLog(value = "查询视图差异", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryAllViewDiff(@RequestParam(name = "sourceId1", required = true) Long sourceId1,
                                   @RequestParam(name = "sourceId2", required = true) Long sourceId2,
                                   String viewName, Integer diff) {
        viewName=StringUtils.toUpperCase(viewName);
        return Result.ok(dbmsViewService.compareView(sourceId1, sourceId2, StringUtils.convertString(viewName,true), diff));
    }

    @GetMapping(value = "/queryCompareViewContent")
    @ApiOperation("查询对比视图")
    @AutoLog(value = "查询对比视图", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryCompareViewContent(@RequestParam(name = "sourceId1", required = true) Long sourceId1,
                                          @RequestParam(name = "sourceId2", required = true) Long sourceId2,
                                          @RequestParam(name = "viewName", required = true) String viewName) {
        viewName=StringUtils.toUpperCase(viewName);
        return Result.ok(dbmsViewService.queryViewCompareContentResult(sourceId1, sourceId2, viewName));
    }

    @GetMapping(value = "/syncView")
    @ApiOperation("同步source1视图到source2")
    @AutoLog(value = "同步source1视图到source2", logType = CommonConstant.LOG_TYPE_ACTION)
    public Result syncView(@RequestParam(name = "sourceId1", required = true) Long sourceId1,
                           @RequestParam(name = "sourceId2", required = true) Long sourceId2,
                           @RequestParam(name = "viewName", required = true) String viewName) throws Exception{
        viewName=StringUtils.toUpperCase(viewName);
        dbmsViewService.syncView(sourceId1,sourceId2,viewName);
        return Result.ok("同步成功");
    }

}
